package com.yhgm.controller;

import com.yhgm.annotation.AuthCheck;
import com.yhgm.common.BaseResponse;
import com.yhgm.common.DeleteRequest;
import com.yhgm.common.ErrorCode;
import com.yhgm.common.ResultUtils;
import com.yhgm.constant.UserConstant;
import com.yhgm.exception.BusinessException;
import com.yhgm.exception.ThrowUtils;
import com.yhgm.model.dto.permission.PermissionAddRequest;
import com.yhgm.model.dto.permission.PermissionUpdateRequest;
import com.yhgm.model.entity.Permissions;
import com.yhgm.model.vo.PermissionVO;
import com.yhgm.service.PermissionsService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yh
 * @Date: 2024/3/24
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    private PermissionsService permissionsService;

    /**
     * 获取权限列表信息
     * @return
     */
    @GetMapping("/get/list")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<List<PermissionVO>> getPermissionList() {
        List<Permissions> permissionsList = permissionsService.list();
        List<PermissionVO> permissionVOList = new ArrayList<>();
        for (Permissions permissions : permissionsList) {
            PermissionVO permissionVO = new PermissionVO();
            BeanUtils.copyProperties(permissions, permissionVO);
            permissionVOList.add(permissionVO);
        }
        return ResultUtils.success(permissionVOList);
    }

    /**
     * 权限点添加
     * @param permissionAddRequest
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addPermission(@RequestBody PermissionAddRequest permissionAddRequest) {
        if (permissionAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Permissions permissions = new Permissions();
        BeanUtils.copyProperties(permissionAddRequest, permissions);
        boolean result = permissionsService.save(permissions);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(permissions.getId());
    }

    /**
     * 根据id获取权限点具体信息
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<PermissionVO> getPermissionVOById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Permissions permissions = permissionsService.getById(id);
        ThrowUtils.throwIf(permissions == null, ErrorCode.NOT_FOUND_ERROR);
        PermissionVO permissionVO = new PermissionVO();
        BeanUtils.copyProperties(permissions, permissionVO);
        return ResultUtils.success(permissionVO);
    }

    /**
     * 修改权限
     * @param permissionUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updatePermission(@RequestBody PermissionUpdateRequest permissionUpdateRequest) {
        if (permissionUpdateRequest == null || permissionUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Permissions permissions = new Permissions();
        BeanUtils.copyProperties(permissionUpdateRequest, permissions);
        boolean result = permissionsService.updateById(permissions);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 删除权限点
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deletePermission(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = permissionsService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }
}
